﻿using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Topology;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DotSpatialExcelAddIn
{
    class kUtilitySpatialFunction
    {
        kUtilityFunction util = new kUtilityFunction();
        public void createNodeShapefile(string filename, DataGridView dt, Map kMap)
        {
            FeatureSet fs;
            fs = new FeatureSet(FeatureType.Point);
            //---------------------------------------------------------
            for (int i = 0; i < dt.Columns.Count ; i++) 
            {
                fs.DataTable.Columns.Add(new DataColumn(dt.Columns[i].HeaderText, typeof(string)));
            }
            //---------------------------------------------------------
            int xCol = -1;
            int yCol = -1;
            int nodeTyp = -1;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Columns[i].HeaderText == "X") xCol = i;
                if (dt.Columns[i].HeaderText == "Y") yCol = i;
                if (dt.Columns[i].HeaderText == "NodeType") nodeTyp = i;
            }


            for (int i = 0; i < dt.Rows.Count ; i++) //Point shape
            {
                //string  TYP = dt.Rows[i].Cells[nodeTyp].Value.ToString();
                double x = Convert.ToDouble(dt.Rows[i].Cells[xCol].Value);
                double y = Convert.ToDouble(dt.Rows[i].Cells[yCol].Value);
                if (x != 0 & y!=0)
                {
                    Coordinate node = new Coordinate(x, y);
                    IPoint nodeFe = new DotSpatial.Topology.Point(node);
                    IFeature ifea = fs.AddFeature(nodeFe);
                    //------------------------------------------------------
                    ifea.DataRow.BeginEdit();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ifea.DataRow[j] = dt.Rows[i].Cells[j].Value;
                    }
                    ifea.DataRow.EndEdit();
                }
            } // next alignment shape

            fs.Projection = kMap.Projection;
            
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            fs.Name = "Node";
            fs.Filename = filename;
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


            fs.SaveAs(fs.Filename, true);
            //fs.Save();
            
            util.removeDupplicateLyr(fs.Name, kMap);

            kMap.Layers.Add(fs.Filename);
            
            //loadLayerList();
            //pvMap.MapFrame.DrawingLayers.Clear();
            util.ClearGraphicMap(kMap);
        }

        public void createLinkShapefile(string filename, DataGridView dt, FeatureSet fe, Map kMap, bool AirDistance= false )
        {
            if (fe.NumRows()>dt.Rows.Count)
            {
                MessageBox.Show("Number of feature error"); 
                return; 
            }
            
            FeatureSet fs;
            fs = new FeatureSet(FeatureType.Line);
            //---------------------------------------------------------
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                fs.DataTable.Columns.Add(new DataColumn(dt.Columns[i].HeaderText, typeof(string)));
            }
            //---------------------------------------------------------

            for (int i = 0; i < fe.NumRows(); i++) //Point shape
            {
                IFeature RoadFe = fe.GetFeature(i);
                if (AirDistance == true)
                {
                    Coordinate[] L = new Coordinate[2]; //x-axis

                    L[0] = new Coordinate(RoadFe.Coordinates[0]);
                    L[1] = new Coordinate(RoadFe.Coordinates[RoadFe.NumPoints-1]);

                    LineString ls = new LineString(L);
                    IFeature feLink = new Feature(ls);
                    IFeature ifea = fs.AddFeature(feLink);
                    ifea.DataRow.BeginEdit();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ifea.DataRow[j] = dt.Rows[i].Cells[j].Value;
                    }
                    ifea.DataRow.EndEdit();
                }
                else
                {
                    IFeature ifea = fs.AddFeature(RoadFe);
                    ifea.DataRow.BeginEdit();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        ifea.DataRow[j] = dt.Rows[i].Cells[j].Value;
                    }
                    ifea.DataRow.EndEdit();                
                }

                //------------------------------------------------------
            } // next alignment shape

            fs.Projection = kMap.Projection;

            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            fs.Name = "Link";
            fs.Filename = filename;
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            
            fs.SaveAs(fs.Filename, true);
            util.removeDupplicateLyr(fs.Name, kMap);

            kMap.Layers.Add(fs.Filename);

            //loadLayerList();
            //pvMap.MapFrame.DrawingLayers.Clear();
            //util.ClearGraphicMap(kMap);
        }

    }
}
